from django.shortcuts import render, HttpResponse, redirect
from app01.models import UserInfo, Department


# Create your views here.
def index(request):
    return HttpResponse("欢迎使用")
    
def user_list(request):
    #to look for a html file under app's templates folder
    return render(request, "user_list.html")    
    
def user_add(request):
    #return HttpResponse("用户添加")
    return render(request, "user_add.html")        

def tpl(request):
    name = "昊天"
    roles = ['运维', 'CEO', '开发']
    user_info = {'name': '陈昊天', 'salary': 100000, 'role': 'CTO'}
    data_list = [
        {'name': '陈昊天', 'salary': 100000, 'role': 'CTO'},
        {'name': '陈星佑', 'salary': 100000, 'role': 'CTO'},
        {'name': '陈星妍', 'salary': 100000, 'role': 'CTO'}
    ]
    return render(request, "tpl.html", {"n1":name, "n2": roles, "n3": user_info, "n4": data_list})


def news(req):
    import requests
    res = requests.get('http://www.chinaunicom.com.cn/api/article/NewsByIndex/2/2021/11/news')
    data_list = res.json()
    print(data_list)
    return render(req, "news.html", {"news_list": data_list})


def something(request):
    # request 是一个对象，封装了用户发送过来的请求相关的数据
    # 1.获取请求方式 GET 或 POST
    print(request.method)

    # 2.在URL上传递值 /something/?n1=123&n2=999
    print(request.GET)

    # 3.在请求体中提交数据
    print(request.POST)

    # 4.[响应] HttpResponse('返回内容') 字符串内容返回给请求者
    # HttpResponse('返回内容')

    # 5.[响应] 读取HTML的内容 + 渲染（替换）-> 字符串，返回给用户浏览器
    # return render(request, 'something.html', {'title': '渴望'})

    # 6.[响应] 让浏览器重定向到其他页面
    return redirect('https://www.baidu.com')


def login(request):
    if request.method == 'GET':
        return render(request, 'login.html')
    else:
        # print(request.POST)
        username = request.POST.get('user')
        password = request.POST.get('pswd')
        if username == 'root' and password == '123':
            # return HttpResponse('登录成功')
            return redirect('http://www.chinaunicom.com.cn')
        else:
            # return HttpResponse('用户名或密码错误')
            return render(request, 'login.html', {'error_msg': '用户名或密码错误'})



def orm(request):
    ## Create
    # Department.objects.create(title='销售部')
    # Department.objects.create(title='IT部')
    # Department.objects.create(title='运营部')
    # UserInfo.objects.create(name='武沛奇', password='123', age=19)
    # UserInfo.objects.create(name='陈昊天', password='666', age=28)
    # UserInfo.objects.create(name='陈星海', password='666')

    ## Delete
    # UserInfo.objects.filter(id=3).delete()

    ## Retrieve
    # data_list = [row, row, row]  QuerySet类型
    # data_list = UserInfo.objects.all()
    # print(data_list)
    # for obj in data_list:
    #     print(obj.id, obj.name, obj.password, obj.age)

    # obj = UserInfo.objects.filter(id=1).first()
    # print(obj.id, obj.name, obj.password, obj.age)

    ## Update
    # UserInfo.objects.all().update(password='999')
    # UserInfo.objects.filter(name='陈昊天').update(age=20)

    return HttpResponse('成功')


def info_list(request):
    user_list = UserInfo.objects.all()
    print(user_list)
    return render(request, 'info_list.html', {'data_list': user_list})


def info_add(request):
    if request.method == 'GET':
        return render(request, 'info_add.html')
    user = request.POST.get('user')
    pwd = request.POST.get('pwd')
    age = request.POST.get('age')
    UserInfo.objects.create(name=user, password=pwd, age=age)
    # return HttpResponse('添加成功')
    return redirect('/info/list/')


def info_delete(request):
    nid = request.GET.get('nid')
    UserInfo.objects.filter(id=nid).delete()
    return redirect('/info/list/')